import { createRouter, createWebHashHistory } from 'vue-router'
import tipUtil from "@/utils/tipUtil";

const routes = [
  //-----------------------------------------------商品展示面板-----------------------------------------------
  {
    path: '/',
    name: 'FarmCropsHome',
    component: () => import('@/views/farm-crops/FarmCropsHome'),
  },
  {
    path: '/farmCropsList',
    name: 'FarmCropsList',
    component: () => import('@/views/farm-crops/FarmCropsList'),
  },
  //-----------------------------------------------进入商品详情-----------------------------------------------
  {
    path: '/commodityDetails',
    name: 'CommodityDetails',
    meta: {
      login_require: true,
    },
    component: () => import('@/views/farm-crops/CommodityDetails'),
    children: []
  },
  //-----------------------------------------------用户操纵面板-----------------------------------------------
  {
    path: '/user/userInfo',
    name: 'UserInfo',
    meta: {
      login_require: true,
    },
    component: () => import('@/views/user/UserInfo'),
  },
  {
    path: '/user/userAddress',
    name: 'UserAddress',
    meta: {
      login_require: true,
    },
    component: () => import('@/views/user/UserAddress'),
  },
  {
    path: '/book/shopCar',
    name: 'ShopCar',
    meta: {
      login_require: true,
    },
    component: () => import('@/views/book/ShopCar'),
  },
  {
    path: '/book/bookList',
    name: 'BookList',
    meta: {
      login_require: true,
    },
    component: () => import('@/views/book/BookList'),
  },
  {
    path: '/book/bookShippingList',
    name: 'BookShippingList',
    meta: {
      login_require: true,
    },
    component: () => import('@/views/book/BookShippingList'),
  },
  {
    path: '/book/bookHarvest',
    name: 'BookHarvest',
    meta: {
      login_require: true,
    },
    component: () => import('@/views/book/BookHarvest'),
  },
  {
    path: '/book/bookFinish',
    name: 'BookFinish',
    meta: {
      login_require: true,
    },
    component: () => import('@/views/book/BookFinish'),
  },
  {
    path: '/book/bookDetails',
    name: 'BookDetails',
    meta: {
      login_require: true,
    },
    component: () => import('@/views/book/BookDetails'),
  },
  {
    path: '/book/bookOrigin',
    name: 'BookOrigin',
    meta: {
      login_require: true,
    },
    component: () => import('@/views/book/BookOrigin'),
  },
  //-----------------------------------------------商户操纵面板-----------------------------------------------
  {
    path: '/user/merchantInfo',
    name: 'MerchantInfo',
    meta: {
      login_require: true,
      merchant: true,
    },
    component: () => import('@/views/user/MerchantInfo'),
  },
  {
    path: '/commodity/addCommodity',
    name: 'AddCommodity',
    meta: {
      login_require: true,
      merchant: true,
    },
    component: () => import('@/views/merchant/AddCommodity'),
  },
  {
    path: '/commodity/manageCommodity',
    name: 'ManageCommodity',
    meta: {
      login_require: true,
      merchant: true,
    },
    component: () => import('@/views/merchant/ManageCommodity'),
  },
  {
    path: '/commodity/updateCommodity',
    name: 'UpdateCommodity',
    meta: {
      login_require: true,
      merchant: true,
    },
    component: () => import('@/views/merchant/UpdateCommodity'),
  },
  {
    path: '/commodity/commodityShipment',
    name: 'CommodityShipment',
    meta: {
      login_require: true,
      merchant: true,
    },
    component: () => import('@/views/merchant/CommodityShipment'),
  },
  //-----------------------------------------------共用账户注销面板-----------------------------------------------
  {
    path: '/account/AccountRemoval',
    name: 'AccountRemoval',
    meta: {
      login_require: true,
      merchant: false,
    },
    component: () => import('@/views/user/AccountRemoval'),
  },
  //-----------------------------------------------蔬菜类-----------------------------------------------
  {
    path: '/vegetable',
    name: 'BaseVegetables',
    meta: {
      login_require: false,
    },
    component: () => import('@/views/farm-crops/vegetable/BaseVegetables'),
    children: []
  },
  //-----------------------------------------------瓜果类-----------------------------------------------
  {
    path: '/fruit',
    name: 'BaseFruit',
    meta: {
      login_require: false,
    },
    component: () => import('@/views/farm-crops/fruit/BaseFruit'),
    children: []
  },
  //-----------------------------------------------谷物类-----------------------------------------------
  {
    path: '/cereal',
    name: 'BaseCereal',
    meta: {
      login_require: false,
    },
    component: () => import('@/views/farm-crops/cereal/BaseCereal'),
    children: []
  },
  //-----------------------------------------------菌类-----------------------------------------------
  {
    path: '/edible-fungus',
    name: 'BaseEdibleFungus',
    meta: {
      login_require: false,
    },
    component: () => import('@/views/farm-crops/edible-fungus/BaseEdibleFungus'),
    children: []
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.matched.some(function (item) {
    return item.meta.login_require && localStorage.getItem("token") === null
  })) {
    tipUtil.errTip("您还未登录！请先登录！");
    next('/')
  } else
    next()
})

export default router
